//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by 谭仁川 on 15/11/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let maxImageCount = 4
class NewFeatureViewController: UICollectionViewController {

    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

 
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

       
    }



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return maxImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index = indexPath.item
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        if indexItem == maxImageCount - 1 {
            cell.startAnimation()
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    var index: Int = 0 {
    
        didSet {
        iconView.image = UIImage(named: "new_feature_\(index + 1)")
                startBtn.hidden = index != 3
                startBtn.hidden = true
        
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    func startAnimation(){
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    
    }
    
    @objc private func startBtnDidClick(){
    NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
        
    
    }
    
    //懒加载字视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named:"new_feature_1"))
    private lazy var startBtn:UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        
        btn.sizeToFit()
        return btn
        }()
    
    
    
}
